const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    scrollleft: 0,
    currentTab: 0,
    navHeight: 0,
    openLineFlag: null,
    coverFlag: false,
    coverAbruptFlag: false,
    coverGrouFlag: false,
    coverCarInfoFlag: false,
    // carTypeSelect: '',
    openLineData: [{
      star: '汽贸城',
      end: '高铁线',
      time: '07:40',
      km: '2km',
      upTrain: '万科白马花园东门',
      nextTrain: ' 临空SOHO',
      butflag: true,
      lineflag: false,
      item: [{
        name: '会展中心'
      }, {
        name: '万里城',
        seat: '2.5km'
      }, {
        name: '检测中心'
      }, {
        name: '目的地1'
      }]
    },
    {
      star: '汽车城',
      end: '虹桥线',
      time: '07:40',
      km: '3km',
      upTrain: '万科白马花园东门',
      nextTrain: ' 临空SOHO',
      butflag: false,
      lineflag: false,
      item: [{
        name: '会展中心2'
      }, {
        name: '万里城',
        seat: '3.5km'
      }, {
        name: '检测中心'
      }, {
        name: '目的地2'
      }]
    }
    ],
    carTypeData: [{
      volume: '20',
      price: '840',
      num: 0,
      src: '../../images/index/bus1.png',
      srcActive: '../../images/index/bus1_active.png'
    },
    {
      volume: '30',
      price: '890',
      num: 0,
      src: '../../images/index/bus2.png',
      srcActive: '../../images/index/bus2_active.png'
    },
    {
      volume: '46',
      price: '1000',
      num: '0',
      src: '../../images/index/bus3.png',
      srcActive: '../../images/index/bus3_active.png'
    }
    ],
    carPrice: 0,
    goTime: '',
    nextTime: '',
    carInfoData: {
      goPoint: '',
      nextPoint: '',
      departureTime: '',
    },
    seatInfoData: {
      seatNum: '',
      contact: '',
      phone: ''
    },
    point: 'goPoint',
    longitude: '',
    latitude: '',

    multiArray: [
      ['今天', '明天', '后天'],
      ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24',],
      ['00', '10', '20', '30', '40', '50', '60']],

    shiftIndex: [0, 0, 0],
    animationData: {},
  },

  bindMultiPickerColumnChange: function (e) {
    console.log('修改的列为', e.detail.column, '，值为', e.detail.value);

    let multiArray = this.data.multiArray;
    let shiftIndex = this.data.shiftIndex;
    let departureTime = multiArray[0][shiftIndex[0]] + ' ' + multiArray[1][shiftIndex[1]] + ':' + multiArray[2][shiftIndex[2]]
    var data = {
      multiArray: this.data.multiArray,
      shiftIndex: this.data.shiftIndex,
      ['carInfoData.departureTime']: departureTime
    };
    data.shiftIndex[e.detail.column] = e.detail.value;
    this.setData(data);
  },
  gobindPickerChange: function (e) {
    let _that = this
    _that.setData({
      goTime: e.detail.value,
    })
  },
  nextbindPickerChange: function (e) {
    let _that = this
    _that.setData({
      nextTime: e.detail.value,
    })
  },
  timeBindPickerChange: function (e) {
    let _that = this
    let param = 'carInfoData.departureTime'
    _that.setData({
      [param]: e.detail.value,
    })
  },
  /** 
   * 滑动切换tab 
   */
  bindChange: function (e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
    this.checkCor();
  },

  checkCor: function () {
    if (this.data.currentTab > 4) {
      this.setData({
        scrollleft: 300
      })
    } else {
      this.setData({
        scrollleft: 0
      })
    }
  },
  /** 
   * 点击tab切换 
   */
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  /**
   * 展开路线图
   */
  openLine: function (e) {
    let _that = this
    let index = e.currentTarget.dataset.index
    let lineflag = 'openLineData[' + index + '].lineflag'
    _that.setData({
      [lineflag]: !_that.data.openLineData[index].lineflag
    })
  },
  madeRouter() {
    let _that = this
    _that.setData({
      coverFlag: !_that.data.coverFlag
    })
  },
  madeAbruptRouter() {
    let _that = this
    _that.setData({
      coverAbruptFlag: !_that.data.coverAbruptFlag
    })
  },
  madeGroudRouter() {
    let _that = this
    _that.setData({
      coverGrouFlag: !_that.data.coverGrouFlag
    })
  },
  madeCarInfoRouter() {
    let _that = this
    console.log(123)
    _that.setData({
      coverCarInfoFlag: !_that.data.coverCarInfoFlag
    })
  },
  looAllLine: function () {
    wx.navigateTo({
      url: '/pages/line/line',
    })
  },
  routedetails() {
    wx.navigateTo({
      url: '/pages/routedetails/routedetails',
    })
  },
  goLine() {
    wx.navigateTo({
      url: '/pages/routedetails/routedetails',
    })
  },
  signUp(e) {
    let _that = this
    let index = e.currentTarget.dataset.index
    let butflag = 'openLineData[' + index + '].butflag'
    _that.setData({
      [butflag]: !_that.data.openLineData[index].butflag
    })
  },

  // 加减车型
  carEdit(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let flag = e.currentTarget.dataset.flag;
    let carTypeData = that.data.carTypeData;
    let num = that.data.carTypeData[index].num;
    let param = 'carTypeData[' + index + '].num';
    let sum = 0;
    let val = 0;
    if (flag === 'add') {
      that.setData({
        [param]: ++num,
      })
    } else if (flag === 'del') {
      that.setData({
        [param]: --num,
      })
    } else {
      num != 0 ? val = 0 : val = 1
      that.setData({
        [param]: val
      })
    }
    carTypeData.forEach(item => {
      sum += item.price * item.num;
      that.setData({
        carPrice: sum
      })
    });
  },
  // 处理 input 数据双向绑定
  handleInputChange: function (e) {
    // 取出定义的变量名
    let name = e.currentTarget.dataset.modal;
    // 取出实时的变量值
    let value = e.detail.value;
    // 定义一个键值对
    let data = 'carInfoData.' + name
    // 刷新数据
    this.setData({
      [data]: value
    });
  },
  handleTwoInputChange: function (e) {
    // 取出定义的变量名
    let name = e.currentTarget.dataset.modal;
    // 取出实时的变量值
    let value = e.detail.value;
    // 定义一个键值对
    let data = 'seatInfoData.' + name
    // 刷新数据
    this.setData({
      [data]: value
    });
  },
  handleInputInput: function (e) {
    wx.navigateTo({
      url: '/pages/trainmap/trainmap?point=' + e.currentTarget.dataset.modal + '&latitude=' + this.data.latitude + '&longitude=' + this.data.longitude + '&lastVal=' + this.data.carInfoData.goPoint,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var qqmapsdk = require('../../utils/qqmap-wx-jssdk.min.js');

    // 实例化API核心类
    qqmapsdk = new qqmapsdk({
      key: 'BN3BZ-2KU3D-GQY4H-HBG6C-DV3O2-4RFJX'
    });
    /** 
     * 获取系统信息 
     */
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight,
          navHeight: app.globalData.navHeight + 1,
        });
      }
    });
    wx.getLocation({
      type: 'gcj02',
      // isHighAccuracy: true,
      success(res) {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
        });
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function (addressRes) {
            console.log(addressRes)
            var address = addressRes.result.address; // 描述
            var recommend = addressRes.result.formatted_addresses.recommend; // 描述
            that.setData({
              ['carInfoData.goPoint']: recommend
            })
          }
        })
      }
    });

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (e) {
    let _that = this
    let pages = getCurrentPages(); //获取当前页面js里面的pages里的所有信息。
    var currPage = pages[pages.length - 1];
    if (currPage.data.point === 'goPoint') {
      _that.setData({
        ['carInfoData.goPoint']: currPage.data.goPoint,
        latitude: currPage.data.latitude,
        longitude: currPage.data.longitude,
      });
    } else if (currPage.data.point === 'nextPoint') {
      _that.setData({
        ['carInfoData.nextPoint']: currPage.data.nextPoint,
        latitude: currPage.data.latitude,
        longitude: currPage.data.longitude,
      });
    }
  }
})